#include "EEPROMService.h"

int EEPROMService::read(EEPROMRequest& request)
{
    if (request.dataLen == 0) {
        return 0;
    }
    Wire.beginTransmission(request.deviceAddr);
    int addr = request.getAddr();
    Wire.write(addr >> 8);
    Wire.write(addr & 0xFF);

    int errorCode = Wire.endTransmission();
    if (errorCode != 0)
    {
        return errorCode;
    }

    Wire.requestFrom(request.deviceAddr, request.dataLen);
    
    Wire.readBytes(request.data, request.dataLen);
}

int EEPROMService::write(EEPROMRequest& request)
{
    if (request.dataLen == 0) {
        return 0;
    }

    int addr = request.getAddr();
    Wire.beginTransmission(request.deviceAddr);
    Wire.write(addr >> 8);
    Wire.write(addr & 0xFF);
    Wire.write(request.data, request.dataLen);

    return Wire.endTransmission();
}

EEPROMService::~EEPROMService()
{
}

EEPROMService eepromService;